import 'package:flutter/material.dart';
import './src/catalog.dart';
import './src/item_tile.dart';
import 'package:provider/provider.dart';

void main(List<String> args) {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<Catalog>(
      create: (context) => Catalog(),
      child: MaterialApp(title: 'Infinite List Sample', home: MyHomePage()),
    );
  }
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Infinite List Sample')),
      body: Selector<Catalog, int?>(
        builder: (context, itemCount, child) => ListView.builder(
          itemBuilder: (context, index) {
            var catalog = Provider.of<Catalog>(context);
            var item = catalog.getByIndex(index);
            if (item.isLoading) {
              return const LoadingItemTile();
            }
            return ItemTile(item: item);
          },
          itemCount: itemCount,
          padding: EdgeInsets.symmetric(vertical: 18),
        ),
        selector: (context, catalog) => catalog.itemCount,
      ),
    );
  }
}
